package xyz.thoughtset.viewer.common.crud.api.controller;


import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/c/")
public class CommandController extends CrudController {

    @PostMapping(value = "insert/{subject}" , produces = MediaType.APPLICATION_JSON_VALUE)
    public Object insert(@RequestBody LinkedHashMap<String, Object> reqMap, @PathVariable("subject") String subject){
        Object result = findCommandService(subject,"insert").createData(reqMap);
        return saveOptLog(subject,"insert",result);
    }

    @PostMapping(value = "update/{subject}" , produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(@RequestBody LinkedHashMap<String, Object> reqMap, @PathVariable("subject") String subject){
        Object result = findCommandService(subject,"update").updateData(reqMap);
        return saveOptLog(subject,"update",result);
    }

    @PostMapping(value = "save/{subject}" , produces = MediaType.APPLICATION_JSON_VALUE)
    public Object save(@RequestBody LinkedHashMap<String, Object> reqMap, @PathVariable("subject") String subject){
        Object result = findCommandService(subject,"save").saveData(reqMap);
        return saveOptLog(subject,"save",result);
    }

    @PostMapping(value = "delete/{subject}")
    public Object delete(@RequestParam String pkey, @PathVariable("subject") String subject) throws Exception {
        Object result = findCommandService(subject,"delete").deleteById(pkey);
        return saveOptLog(subject,"delete",pkey,pkey);
    }

    @PostMapping(value = "removeByIds/{subject}")
    public Object removeByIds(@RequestBody List<String> idList, @PathVariable("subject") String subject) throws Exception {
        return findCommandService(subject,"removeList").removeByIds(idList);
    }

}
